var Calculator = {

  XUL_FILE : "chrome://calculator/content/",
  APP_NAME : "Calculator",
  PREF_NAME_CALC_OPEN : "calc_open",

  _branch : Components.classes["@mozilla.org/preferences-service;1"]
            .getService(Components.interfaces.nsIPrefService)
            .getBranch("calculator."),

  newCalculator : function() {
    // Open the calc with the UI set in the prefs
    var truc = this.getPreference(this.PREF_NAME_CALC_OPEN, "Int", 0);
    if (truc == 0) {
      this.newCalculatorTab();
    }
    else if (truc == 1) {
      this.newCalculatorWindow();
    }
    else if (truc == 2) {
      this.newCalculatorSidebar();
    }
  },

  newCalculatorTab : function() {
    var newTab = gBrowser.addTab(this.XUL_FILE);
    newTab.setAttribute(this.APP_NAME, 'true');
    newTab.label = this.APP_NAME;
    getBrowser().selectedTab = newTab;
  },

  newCalculatorWindow : function() {
    window.open(this.XUL_FILE, "_blank", "chrome,all,dialog=no,resizable=yes");
  },

  newCalculatorSidebar: function() {
    toggleSidebar('viewCalculatorSidebar');
  },


  getPreference : function(aPref, aType, aDefault) {
    try {
      var val = eval("this._branch.get" + aType + "Pref('" + aPref + "')");
      return val;
    } catch(e) {
      this.setPreference(aPref, aType, aDefault);
      return aDefault;
    }
    return null;
  },

  setPreference : function(aPref, aType, aValue) {
    try {
      eval("this._branch.set" + aType + "Pref('" + aPref + "'," + aValue + ")");
      return true;
    } catch(e) {
      alert(e);
    }
    return false;
  }
}